#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.61)
AC_INIT(ncc, 1.0, mark@heily.com)
AC_CONFIG_SRCDIR([nc_exception.h])
AC_CONFIG_HEADER([config.h])
AM_INIT_AUTOMAKE(ncc, 1.0)

# Checks for programs.
AC_PROG_CC
AC_PROG_LIBTOOL

# Checks for libraries.
# TODO: PKG_CHECK_MODULES([NCLIBDEP], [libssl])
# TODO: PKG_CHECK_MODULES([NCLIBDEP], [apr-1 >= 1.0])

# Checks for header files.
AC_HEADER_DIRENT
AC_HEADER_STDC
AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS([arpa/inet.h fcntl.h inttypes.h limits.h netdb.h netinet/in.h stdlib.h string.h strings.h sys/param.h sys/socket.h sys/time.h syslog.h unistd.h pthread.h arpa/nameser_compat.h execinfo.h])

# Check for libevent 
#
#AC_CHECK_HEADER(event.h,,
#    [AC_MSG_ERROR([cannot find event.h, please install libevent and run ./configure again])])

# Check if the resolver functions are included in the C library.
# If they are not, -lresolv must be added to LDFLAGS.
#
# Under Linux and Darwin, this is false; under FreeBSD and OpenBSD this is true.
# 
AC_CHECK_LIB(resolv, res_query)
AM_CONDITIONAL(WITH_LIBRESOLV, test x$ac_cv_lib_resolv_res_query = xyes)

# Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_STDBOOL
AC_C_CONST
AC_TYPE_UID_T
AC_C_INLINE
AC_TYPE_INT32_T
AC_TYPE_MODE_T
AC_TYPE_OFF_T
AC_TYPE_SIZE_T
AC_TYPE_SSIZE_T
AC_HEADER_TIME
AC_STRUCT_TM
AC_TYPE_UINT32_T
AC_TYPE_UINT8_T

# Checks for library functions.
AC_FUNC_CHOWN
AC_FUNC_CLOSEDIR_VOID
AC_FUNC_MALLOC
AC_FUNC_MKTIME
AC_FUNC_REALLOC
AC_FUNC_STAT
AC_FUNC_STRFTIME
AC_FUNC_VPRINTF
AC_CHECK_FUNCS([atexit gethostname localtime_r memset mkdir regcomp rmdir strcasecmp strchr strstr strtol strtoul getpwuid_r getpeereid])

# Allow the pkg-config directory to be set
# (Borrowed from libpng)
AC_ARG_WITH(pkgconfigdir,
	AC_HELP_STRING([--with-pkgconfigdir],
	[Use the specified pkgconfig dir (default is libdir/pkgconfig)]),
	[pkgconfigdir=${withval}],
	[pkgconfigdir='${libdir}/pkgconfig'])
AC_SUBST([pkgconfigdir])
AC_MSG_NOTICE([pkgconfig directory is ${pkgconfigdir}])

# Allow the IPC directory to be set
AC_ARG_WITH(ipcdir,
	AC_HELP_STRING([--with-ipcdir],
	[Use the specified IPC dir (default is /var/run)]),
	[ipcdir=${withval}],
	[ipcdir='/var/run'])
AC_SUBST([ipcdir])
AC_MSG_NOTICE([IPC directory is ${ipcdir}])

# Build a threadsafe library by default
AC_DEFINE([_REENTRANT], 1, [Require reentrancy])

# Use the Natural C compiler instead of GCC
GCC=$CC
CC=./ncc
export CC

AC_CONFIG_FILES([Makefile nc_site.h])
AC_OUTPUT([libnc.pc])
